﻿using System;
using TaskManager.Domain.BusinessObjects;
using TaskManager.Domain.Repositories;

namespace TaskManager.Domain.Services
{
    internal class UserActionLogger : IUserActionLogger
    {
        private readonly IProjectLogWriter _projectLogWriter;
        private readonly ITaskLogWriter _taskLogWriter;
        private readonly User _currentUser;

        public UserActionLogger(IProjectLogWriter projectLogWriter, ITaskLogWriter taskLogWriter, User currentUser)
        {
            _projectLogWriter = projectLogWriter;
            _taskLogWriter = taskLogWriter;
            _currentUser = currentUser;
        }

        /// <summary>
        /// Logs task reassignment
        /// </summary>
        /// <param name="task">Task</param>
        /// <param name="oldAssignee">Old assignee</param>
        /// <param name="newAssignee">New assignee</param>
        public void LogReassignTask(Task task, string oldAssignee, string newAssignee)
        {
            string message = string.Format(Resource.UserActionLogger_LogReassignTask, oldAssignee, newAssignee);
            _taskLogWriter.Write(new TaskLogEntry(task, _currentUser, message));
        }

        /// <summary>
        /// Logs event when owner of a task was changed
        /// </summary>
        /// <param name="task">Task</param>
        /// <param name="oldOwner">Old owner</param>
        /// <param name="newOwner">New owner</param>
        public void LogChangeTaskOwner(Task task, string oldOwner, string newOwner)
        {
            string message = string.Format(Resource.UserActionLogger_LogChangeTaskOwner_Owner_changed, oldOwner, newOwner);
            _taskLogWriter.Write(new TaskLogEntry(task, _currentUser, message));
        }

        /// <summary>
        /// Logs task start event
        /// </summary>
        /// <param name="task">Task</param>
        public void LogStartTask(Task task)
        {
            string message = string.Format(Resource.UserActionLogger_LogStartTask, _currentUser.Name);
            _taskLogWriter.Write(new TaskLogEntry(task, _currentUser, message));
        }

        /// <summary>
        /// Logs task delete event
        /// </summary>
        /// <param name="project">Project</param>
        /// <param name="taskSummary">Task summary</param>
        public void LogDeleteTask(Project project, string taskSummary)
        {
            string message = string.Format(Resource.UserActionLogger_LogDeleteTask, taskSummary, _currentUser.Name);
            _projectLogWriter.Write(new ProjectLogEntry(project, _currentUser, message));
        }

        /// <summary>
        /// Logs task creation
        /// </summary>
        /// <param name="task">Task</param>
        public void LogCreateTask(Task task)
        {
            string message = string.Format(Resource.UserActionLogger_LogCreateTask, _currentUser.Name);
            _taskLogWriter.Write(new TaskLogEntry(task, _currentUser, message));
        }

        /// <summary>
        /// Logs task reopen operation
        /// </summary>
        /// <param name="task"></param>
        public void LogReopenTask(Task task)
        {
            string message = string.Format(Resource.UserActionLogger_LogReopenTask, _currentUser.Name);
            _taskLogWriter.Write(new TaskLogEntry(task, _currentUser, message));
        }

        /// <summary>
        /// Logs project creation
        /// </summary>
        /// <param name="project">Project</param>
        public void LogCreateProject(Project project)
        {
            var projectLogEntry = new ProjectLogEntry(project, _currentUser, Resource.UserActionLogger_LogCreateProject);
            _projectLogWriter.Write(projectLogEntry);
        }

        /// <summary>
        /// Logs project close event
        /// </summary>
        /// <param name="project">Project</param>
        public void LogCloseProject(Project project)
        {
            var projectLogEntry = new ProjectLogEntry(project, _currentUser, Resource.UserActionLogger_LogCloseProject);
            _projectLogWriter.Write(projectLogEntry);
        }

        /// <summary>
        /// Logs project reopen event
        /// </summary>
        /// <param name="project">Project</param>
        public void LogReopenProject(Project project)
        {
            var projectLogEntry = new ProjectLogEntry(project, _currentUser, Resource.UserActionLogger_LogReopenProject);
            _projectLogWriter.Write(projectLogEntry);
        }

        /// <summary>
        /// Logs event when new comment was added to task
        /// </summary>
        /// <param name="task">Task</param>
        /// <param name="comment">Comment</param>
        public void LogAddComment(Task task, Comment comment)
        {
            var taskLogEntry = new TaskLogEntry(task, _currentUser, Resource.UserActionLogger_LogAddComment);
            _taskLogWriter.Write(taskLogEntry);
        }

        /// <summary>
        /// Logs task completion
        /// </summary>
        /// <param name="task"></param>
        public void LogCompleteTask(Task task)
        {
            var taskLogEntry = new TaskLogEntry(task, _currentUser, Resource.UserActionLogger_LogCompleteTask);
            _taskLogWriter.Write(taskLogEntry);

            var projectLogEntry = new ProjectLogEntry(task.Project, _currentUser, Resource.UserActionLogger_LogCompleteTask);
            _projectLogWriter.Write(projectLogEntry);
        }
    }
}